package com.javabasic.algorithm.sword_of_offer;

import com.javabasic.algorithm.leetcode.LengthOfLastWord;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * @since Created by work on 2022/1/17 10:51 下午
 */
public class 剑指_Offer_21_调整数组顺序使奇数位于偶数前面 {

    /**
     * 双指针交换数据、类似快速排序基准
     * @param nums
     * @return
     */
    public int[] exchange(int[] nums) {
        int len = nums.length;
        int left = 0, right = len-1, temp;
        while (left < right) {
            while (left < len && (nums[left]&1) == 1) {
                left++;
            }
            while (right >= 0 && (nums[right]&1) == 0) {
                right--;
            }
            if (left < right) {
                temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp;
            }
        }
        return nums;
    }
}
